﻿using System;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace DotNetImage.Imaging.Filters
{
    public enum FlipDirection
    {
        Horizontal = 0,
        Vertical
    }


    public class FlipFilter<TPixel, TColorChannel> : FilterBase
    {
        public FlipFilter() { HasMultiThreadingSupport = true; RequiresInternalRefresh = true; }

        [ParameterAttribute(Name = "Flip", DefaultValue = (double)FlipDirection.Horizontal)]
        public FlipDirection FlipDirection { get; set; }

        protected override void ProcessPerPixel(DotNetImage bmp)
        {
            TransformedBitmap tb = new TransformedBitmap();
            tb.BeginInit();
            tb.Source = bmp.BitmapSource;

            var transform = new ScaleTransform();
            if (FlipDirection == FlipDirection.Horizontal)
            {
                transform.ScaleX = -1;
            }
            else if (FlipDirection == FlipDirection.Vertical)
            {
                transform.ScaleY = -1;
            }

            tb.Transform = transform;
            tb.EndInit();  // "Transform must be a combination of scales, flips, and 90 degree rotations"
        }
    }
}
